﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yu.Cms.Common;

namespace Yu.Cms.Web.UserCtrl.Admin
{
    public partial class AlbumListD : BaseCtrl
    {
        private int aBigID = 0;
        private string fileName = string.Empty;
        private string filePath = string.Empty;
        private string thumbPath = string.Empty;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            string Act = YuRequest.GetQueryString("act");
            int Id = YuRequest.GetQueryInt("ID", 0);
            string picUrl = YuRequest.GetQueryString("photo");
            if (!IsPostBack)
            {

                if (Act.ToLower() == "add")
                {
                    btnSave.CommandName = "add";
                    this.Add();
                }
                else if(Act.ToLower()=="modify")
                {
                    if (Id != 0)
                    {
                        btnSave.CommandName = "modify";
                        btnSave.CommandArgument = Id.ToString();
                        this.Modify(Id, picUrl);
                    }
                }
            }
        }

        /// <summary>
        /// 添加照片
        /// </summary>
        protected void Add()
        {
            aBigID = Utils.StrToInt(Utils.GetCookie("aBigID"),0);
            if (aBigID != 0)
            {
                fileName = Utils.GetCookie("fileName");
                filePath = Utils.GetCookie("filePath");
                thumbPath = Utils.GetCookie("thumbPath");
                imgPhoto.ImageUrl = thumbPath; //缩略图地址
                Yu.Cms.Model.album model = new Yu.Cms.Model.album();
                model.alBigID = aBigID;
                model.alTitle = fileName;
                model.alPath = filePath;
                model.alThumbPath = thumbPath;
                model.alInfo = "";
                model.pubTime = DateTime.Now;
                int alId = bllalbum.Add(model);
                this.UpdateAlbumTypePage(aBigID, alId);
            }
            else
            {
                //参数错误
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        protected void Modify(int id, string picUrl)
        {
            imgPhoto.ImageUrl = picUrl;
            Yu.Cms.Model.album model = bllalbum.GetModel(id);
            txtTitle.Text = model.alTitle;
            txtInfo.Text = model.alInfo;

        }


        /// <summary>
        /// 更新首页照片
        /// </summary>
        /// <param name="BigID"></param>
        /// <param name="alId"></param>
        protected void UpdateAlbumTypePage(int BigID,int alId)
        {
            Yu.Cms.Model.albumType fam = bllalbumType.GetModel(BigID);
            if (fam.alTopID == 0)
            {
                fam.alTopID = alId;
                fam.alBigID = BigID;
                bllalbumType.Update(fam);
            }
        }

        

        /// <summary>
        /// 确定添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_OnCommand(object sender, CommandEventArgs e)
        {
            string strTitle = YuRequest.GetFormString(txtTitle.UniqueID);
            string strInfo = YuRequest.GetFormString(txtInfo.UniqueID);
            if (e.CommandName == "add")
            {
                Yu.Cms.Model.album model = bllalbum.GetTopModel();
                model.alTitle = strTitle;
                model.alInfo = strInfo;
                model.alkey = "";
                bllalbum.Update(model);
                Response.Redirect("AlbumList.aspx?BigID=" + model.alBigID);
            }
            else if (e.CommandName == "modify")
            {
                Yu.Cms.Model.album model = bllalbum.GetModel(Utils.StrToInt(e.CommandArgument.ToString(),0));
                model.alTitle = strTitle;
                model.alInfo = strInfo;
                model.alkey = "";
                bllalbum.Update(model);
                Response.Redirect("AlbumList.aspx?BigID=" + model.alBigID);
            }

            
            
        }

        /// <summary>
        /// 移除cookies
        /// </summary>
        public override void Dispose()
        {
            Utils.DeleteCookie("aBigID");
            Utils.DeleteCookie("fileName");
            Utils.DeleteCookie("filePath");
            base.Dispose();
        }

       
    }
}